package com.dtlaya.allutils.fileUpload.controller;

import com.dtlaya.allutils.fileUpload.Service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class FileUploadController {

    private final FileUploadService fileUploadService;

    @Autowired
    public FileUploadController(FileUploadService fileUploadService) {
        this.fileUploadService = fileUploadService;
    }

    @PostMapping("/upload")
    public ResponseEntity<Map<String, Object>> uploadFiles(
            @RequestParam("files") MultipartFile[] files,
            @RequestParam(value = "path", defaultValue = "") String path) {
        
        Map<String, Object> response = new HashMap<>();
        
        try {
            // 上传文件
            List<String> savedPaths = fileUploadService.uploadFiles(files, path);
            
            response.put("success", true);
            response.put("message", "文件上传成功");
            response.put("count", savedPaths.size());
            response.put("paths", savedPaths);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "文件上传失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
}